package BizLogic;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;

import dao.BookingDAO;
import dao.DAOFactory;
import model.BookSearchCriteria;
import model.BookingDTO;
import model.HolidayDates;

import java.sql.Date;
import java.sql.SQLException;
public class BookingManager {
	
	private BookingDAO bookingDAO;
	
	public BookingManager(){
		
		bookingDAO = DAOFactory.loadInstance().getBookingDAO();
	}
	public void updateRecord(BookingDTO book)
	{
		bookingDAO.updateBooking(book);
	}
	public BookingDTO findBookDetails(int BookingID)
	{
		BookingDTO book=null;
		try {
			book = bookingDAO.findByBookingID(BookingID);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return book;
	}
	public ArrayList<BookingDTO> findCurrentBookings(){
		ArrayList<BookingDTO> currentBookings = new ArrayList<BookingDTO>();
		try {
			currentBookings = bookingDAO.findAllBooking();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return currentBookings;
	}
	public ArrayList<BookingDTO> getBookingDetails(String StaffID)
	{
		BookingDAO bookDAO=DAOFactory.loadInstance().getBookingDAO();
		ArrayList<BookingDTO> book=null;
		BookSearchCriteria criteria=new BookSearchCriteria();
		criteria.setStaffID(StaffID);
		try {
			book=bookDAO.findByCriteria(criteria);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return book;
		}
	public String bookFacility(BookingDTO bt,String StaffID)
	{
		String result="Booking Successful";
		
		if(bt.getPriorityID()>=3)
		{
			int numberOfBooking=bookingDAO.findNumberOfBooking(bt.getStaffID(),( (bt.getFromDate().getMonth())+1));
			if(numberOfBooking>=3)
			result="Cannot book more than three high prioriy bookings";
		}
		else
		{
			if(detectOverLap( bt.getFromDate(),bt.getToDate(),bt.getFacilityName() ) )
			{
				result="Overlap in selected dates";
			}
			else
			{
				
				try {
					int duration=0;
					bookingDAO.insertBooking(bt);
					String message="Request for "+bt.getFacilityName()+" has been made Approve by ";
					Calendar cal=Calendar.getInstance();
					cal.setTime(new java.util.Date());
					int addDate=3;
					cal.add(Calendar.DATE,addDate);
					if(cal.get(Calendar.DAY_OF_WEEK)==5)
					{
						
						
						addDate+=3;
					}
					HolidayDateManager holi=new HolidayDateManager();
						ArrayList<HolidayDates> holidays=holi.addDuration(cal.DATE,cal.MONTH+1);
						for(HolidayDates h:holidays)
						{
						 int difference=cal.DATE-h.getHolidayStartDate().getDate();
						 difference=h.getHolidayDuration()-difference;
						 if(difference>0)
						 {
							 addDate+=difference;
						 }
						}
						cal.add(Calendar.DATE, 3);
						message+=cal.getTime();
						System.out.println(message);
						StaffManager manage=new StaffManager();
						SendMail send=new SendMail("begalesagar@gmail.com","New Booking Made",message);
					
					
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					result="Problem Booking the facility";
					System.out.println("Exit");
					e.printStackTrace();
				}
			}
				
		}
		return result;
	}
	public boolean detectOverLap(Date FromDate,Date ToDate,String FacilityName)
	{
		boolean status=true;
		BookSearchCriteria criteria=new BookSearchCriteria();
		criteria.setFacilityName(FacilityName);
		criteria.setMonth((FromDate.getMonth())+1);
		ArrayList<BookingDTO> booking=bookingDAO.findByCriteria(criteria);
		Iterator<BookingDTO> i=booking.iterator();
		if(!(booking.size()>0))
			status=false;
		else
		{
		while(i.hasNext())
		{
			BookingDTO bd=i.next();
			if (FromDate.compareTo(bd.getToDate())>=0) {
				status = false;
			} else if (bd.getFromDate().compareTo(ToDate)>=0) {
				status = false;
			}
			return (status);
		}
		}
		return status;
	}
	public int updateStatus(int bookingID,String status)
	{
		int result=0;
		
		try {
			result=bookingDAO.updateBooking(bookingID, status);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
}
